package com.hhkj.api.withdrawal.service.biz.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hhkj.admin.sale.common.cont.SaleWithdrawalConst;
import com.hhkj.admin.sale.domain.SaleWithdrawalAccount;
import com.hhkj.admin.sale.model.dopo.SaleWithdrawalAccountPo;
import com.hhkj.admin.sale.model.res.SaleWithdrawalRes;
import com.hhkj.admin.sale.service.base.SaleWithdrawalAccountService;
import com.hhkj.api.sale.common.session.holder.ApiSaleSessionHolder;
import com.hhkj.api.withdrawal.model.req.ApiWithdrawalAccountReq;
import com.hhkj.api.withdrawal.model.res.ApiWithdrawalAccountRes;
import com.hhkj.api.withdrawal.service.biz.ApiSaleWithdrawalAccountBizService;
import com.ruoyi.common.exception.GlobalException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ApiSaleWithdrawalAccountBizServiceImpl
 *
 * @author zheng
 * @since 2023/11/5 15:47
 */
@Service
public class ApiSaleWithdrawalAccountBizServiceImpl implements ApiSaleWithdrawalAccountBizService {

    @Autowired
    private SaleWithdrawalAccountService accountService;

    @Override
    public ApiWithdrawalAccountRes getById(Long id) {
        SaleWithdrawalAccount saleWithdrawalAccount = accountService.getById(id);
        return BeanUtil.toBean(saleWithdrawalAccount, ApiWithdrawalAccountRes.class);
    }

    @Override
    public boolean removeByIdList(List<Long> idList) {
        idList = CollectionUtil.removeNull(idList);
        return accountService.removeByIds(idList);
    }

    @Override
    public List<ApiWithdrawalAccountRes> list(ApiWithdrawalAccountReq req) {
        req.setSaleId(ApiSaleSessionHolder.getSaleId());
        req.setTypeCode(SaleWithdrawalConst.AccountTypeCode.ALIPAY.getCode());
        List<SaleWithdrawalAccountPo> accountPoList = accountService.listPoByDo(req);
        return BeanUtil.copyToList(accountPoList, ApiWithdrawalAccountRes.class);
    }

    @Override
    public boolean save(ApiWithdrawalAccountReq req) {
        Long saleId = ApiSaleSessionHolder.getSaleId();
        if (hasSameAccount(req.getAccount(), saleId)) {
            throw new GlobalException("该账号已存在在系统中，请确认");
        }
        req.setTypeCode(SaleWithdrawalConst.AccountTypeCode.ALIPAY.getCode());
        req.setSaleId(saleId);
        return accountService.save(req);
    }

    @Override
    public boolean update(ApiWithdrawalAccountReq req) {
        if (ObjectUtil.isEmpty(req.getId())) {
            throw new GlobalException("id不能为空");
        }
        Long saleId = ApiSaleSessionHolder.getSaleId();
        SaleWithdrawalAccount sameAccount = getSameAccount(req.getAccount(), saleId);
        if (ObjectUtil.isNotEmpty(sameAccount) && sameAccount.getId().equals(req.getId()) == false) {
            throw new GlobalException("该账号已存在在系统中，请确认");
        }
        req.setTypeCode(SaleWithdrawalConst.AccountTypeCode.ALIPAY.getCode());
        req.setSaleId(saleId);
        return accountService.updateById(req);
    }

    private boolean hasSameAccount(String account, Long saleId) {
        // 校验是否用重复账号信息
        LambdaQueryWrapper<SaleWithdrawalAccount> repeatQuery = new LambdaQueryWrapper<>();
        repeatQuery.eq(SaleWithdrawalAccount::getAccount, account)
                .eq(SaleWithdrawalAccount::getSaleId, saleId);
        long accountRepeatAmount = accountService.count(repeatQuery);
        if (accountRepeatAmount > 0) {
            return true;
        }
        return false;
    }

    private SaleWithdrawalAccount getSameAccount(String account, Long saleId) {
        // 校验是否用重复账号信息
        LambdaQueryWrapper<SaleWithdrawalAccount> repeatQuery = new LambdaQueryWrapper<>();
        repeatQuery.eq(SaleWithdrawalAccount::getAccount, account)
                .eq(SaleWithdrawalAccount::getSaleId, saleId);
        return accountService.getOne(repeatQuery);
    }

}
